<!DOCTYPE html>
<html>

	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<meta content="telephone=no" name="format-detection" />
		<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1.0,user-scalable=no,viewport-fit=cover" />

		<meta charset="utf-8">
		<script type="text/javascript" src="js/boot.js"></script>
		<title>工号绑定</title>
	</head>

	<body>
		<div id="page">
			<div class="navbar">
				<img class="navbar-left">
				<div class="nav-title">
				</div>
			</div>
			<div class="content">
				<div class="card-cell van-hairline--bottom">
					我的工号
					<input class="cell-input" placeholder="请输入员工工号" type="text" v-model="user.workNo" :value="user.workNo" />
				</div>
				<div class="card-cell van-hairline--bottom">
					我的姓名
					<input class="cell-input" placeholder="请输入员工姓名" type="text" v-model="user.userName" />
				</div>
				<div class="card-cell van-hairline--bottom">
					手机号码
					<input class="cell-input" placeholder="请输入手机号码" type="number" v-model="user.mobile" :value="user.mobile" />
				</div>
				<a :href="'javascript:app.bindingUser();'" class="recharge">
					{{buttonName}}
				</a>
			</div>
		</div>
	</body>

	<script type="text/javascript">
		var openId = localStorage.getItem('openId');
		var app = new Vue({
			el: '#page',
			data: {
				buttonName: "绑定用户",
				user: {
					workNo: '',
					userName: '',
					mobile: ''
				}
			},
			methods: {
				bindingUser() {
					var that = this;
					if(this.buttonName == "绑定用户") {
						var data = this.user;
						data.openId = openId;
						http.post("/weixin/user/bindUser.do", data, function(page) {
							if(page.success) {
								http.get("/user/get.do?openId=" + openId, function(page) {
									if(page.success) {
										var user = page.data;
										// alert(user);
										if(user) {
											that.user = user;
											that.buttonName = "解除绑定";
											alert("绑定成功！");
										}
									}
								});
							} else {
								alert(page.message);
							}
						});
					} else {
						// 解除绑定用户
						http.get("/weixin/user/unBindUser.do?openId=" + openId, function(page) {
							if(page.success) {
								that.user = {};
								that.buttonName = "绑定用户";
								alert("解绑成功！");
							} else {
								alert(page.message);
							}
						});
					}
				}
			},
			created() {
				var that = this;
				http.get("/user/get.do?openId=" + openId, function(page) {
					if(page.success) {
						var user = page.data;
						// alert(user);
						if(user) {
							that.user = user;
							that.buttonName = "解除绑定";
						}
					}
				});
			}
		});
	</script>

</html>